package org.rcy.ruicingmarket.oss.role;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.rcy.framework.api.entity.PageResult;
import org.rcy.ruicingmarket.common.utils.CollectionBeanUtils;
import org.rcy.ruicingmarket.oss.user.OssUser;
import org.rcy.ruicingmarket.oss.user.OssUserDao;
import org.rcy.ruicingmarket.role.BindViewsVo;
import org.rcy.ruicingmarket.role.BindUsersVo;
import org.rcy.ruicingmarket.role.RoleQueryCondition;
import org.rcy.ruicingmarket.role.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/10/9 16:57
 */

@Service
public class RoleService {

	@Autowired
	private RoleDao roleDao;
	@Autowired
	private OssUserDao userDao;
	@Autowired
	private SqlSessionFactory sqlSessionFactory;

	public List<RoleVo> getRolesByUserId(Long userId) {
		return null;
	}

	@Transactional
	public void addRole(RoleVo vo){
		Role role = convertToPo(vo);
		roleDao.insertSelective(role);
	}

	@Transactional
	public void bindUsers(BindUsersVo vo) {
		SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
		RoleDao mapper = sqlSession.getMapper(RoleDao.class);
		vo.getUserIds().stream().forEach(userId -> {
			mapper.bindUser(vo.getRoleId(),userId);
		});
		sqlSession.commit();
		sqlSession.clearCache();
	}

	@Transactional
	public void bindViews(BindViewsVo vo){
		SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
		RoleDao mapper = sqlSession.getMapper(RoleDao.class);
		vo.getViewIds().stream().forEach(viewId -> {
			mapper.bindView(vo.getRoleId(),viewId);
		});
		sqlSession.commit();
		sqlSession.clearCache();
	}

	public PageResult<RoleVo> pageQuery(RoleQueryCondition condition){
		List<OssUser> byUserName = userDao.getByUserName(condition.getUserName());
		if(byUserName.isEmpty()) {
			return new PageResult<RoleVo>(0,10,0,new ArrayList<RoleVo>());
		}
		List<Long> uids = byUserName.stream().map(u -> u.getId()).collect(Collectors.toList());
		List<Long> roleIdsByUids = roleDao.findRoleIdsByUids(uids);
		condition.setIds(roleIdsByUids);

		int count = roleDao.countCondition(condition);
		List<Role> roles = roleDao.pageQuery(condition);
		return new PageResult<>(condition.getPageNo(),condition.getPageSize(),count,
				CollectionBeanUtils.copyListProperties(roles,RoleVo::new,(r,v)->{
					v.setRoleId(r.getId());
					if(!r.getUserList().isEmpty()) {
						StringBuilder userstemp = new StringBuilder();
						for (OssUser ossUser : r.getUserList()) {
							userstemp.append(ossUser.getName() + ",");
						}
						v.setUsers(userstemp.substring(0, userstemp.length() - 1));
					}
				}));
	}

	public List<Role> getRolesByUid(Long uid){
		List<Role> roles = roleDao.getRolesByUserId(uid);
		return roles;
	}

	private Role convertToPo(RoleVo vo){
		Role role = new Role();
		BeanUtils.copyProperties(vo, role);
		return role;
	}

}
